use std::sync::Arc;

use axum::{ Extension, Router, response::Json, routing::get};

use serde_json::{Value, json};
use tracing::info;
use tracing_subscriber::{filter::LevelFilter, 
    fmt::Layer, 
    layer::SubscriberExt, 
    util::SubscriberInitExt, Layer as _};


    struct AppState<'cc> {
        name: &'cc  str,
    }

#[tokio::main]
async fn main(){

    let layer = Layer::new().pretty().with_filter(LevelFilter::INFO);
    tracing_subscriber::registry().with(layer).init();

    

    let shared_state = Arc::new(AppState {name: "hi"});
    let app = Router::new()
    .route("/", get(||async {"Hello world!"}))
    .route("/plain_demo", get(plain_text))
    .route("/json_demo", get(json))
    .with_state(shared_state);
   // .layer(handler(shared_state));

  //  .with_state(shared_state);


    let address = "0.0.0.0:3000";
    let listener = tokio::net::TcpListener::bind(address).await.unwrap();
    info!("listen :{}", address);

    axum::serve(listener, app).await.unwrap();
    info!("service started ")
}

async  fn plain_text() -> &'static str {
    "plain text"
}

async fn json() -> Json<Value> {
    Json(json!({"data": 42}))
}


async fn handler(Extension(state): Extension<AppState>) {

}



